#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>

#define SIZE 11
#define MINENUM 10


char mine[SIZE][SIZE] = {'0'};
char show[SIZE][SIZE] = {'*'};


void Initboard(char (*arr)[SIZE], char ch ){
    for(int i = 0; i < SIZE; i++)
        for(int j = 0; j < SIZE; j++)
            arr[i][j] = ch;//初始化11*11的棋盘，mine棋盘全赋值为'0',show棋盘全赋值为'*'
}

void Print(char (*arr)[SIZE]){
    //打印9*9的棋盘
    for(int i = 0; i < 10; i++)
        printf("%d ", i);//打印行标
    printf("\n");
    for(int i = 1; i < SIZE - 1; i++){
        printf("%d ", i);//打印列标
        for(int j = 1; j < SIZE - 1; j++)
            printf("%c ", arr[i][j]);
        printf("\n");
    } 
}

void Initmine(char (*arr)[SIZE]){
    int x = 0;
    int y = 0;
    for(int i = 0; i < MINENUM; ){
        int x = rand()%(SIZE - 2)+1;
        int y = rand()%(SIZE - 2)+1;//生成两个1~SIZE-2的随机数，分别作为雷的行坐标和列坐标
        if(arr[x][y] != '1'){//避免重复布置雷
            arr[x][y] = '1';//布置雷
            i++;  //确保生成指定雷的个数 
        }
    }
}

int Minenum(int x, int y){
    //返回(x,y)坐标周围雷的总个数
    return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
    mine[x][y - 1] + mine[x][y + 1] + 
    mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'); 
}

int Findmine(){
    
    int x = 0;
    int y = 0;
    int i = pow(SIZE - 2, 2) - MINENUM;//成功扫完雷需要的输入坐标次数
    //Print(mine);
    while(i){               
        Print(show);
        printf("Please enter the (x,y) coordinates of your chosen '*':\n");
        scanf("%d%d", &x, &y);
        if((x < 1 || x > SIZE - 2) || (y < 1 || y > SIZE-2)){
            system("clear");
            printf("Illegal import!Please re-enter:\n");
        }
        else {
            if(mine[x][y] == '1'){
                system("clear");
                printf("You're blown up and the game fails\n");
                Print(mine);
                break;
            }    
            else{//将(x,y)坐标周围雷的总个数呈现出来
                show[x][y] = Minenum(x, y) + '0';
                system("clear");
                i--;
            }
            
        }
    }
    return i;
}
void game(){
    Initboard(mine, '0');
    Initboard(show, '*');
    Initmine(mine);
    if(Findmine(mine) == 0)
        printf("Congratulations! Minesweeper is successful\n");
}

void menu(){
    int n = 0;
    do{
        printf("********************\n");
        printf("**Minesweeper game**\n");
        printf("**     1.play     **\n");
        printf("**     2.exit     **\n");
        printf("********************\n");
        printf("Please enter 1 or 0 to start or exit the game:\n");
        
        scanf("%d", &n);
        switch(n){
            case 1:
                printf("The game begins\n");
                sleep(1);
                system("clear");
                game();
                break;
            case 0:
                printf("Exit the game\n");
                sleep(1);
                system("clear");
                break;
            default:
                printf("The entry is invalid, please reenter\n");
                break;
        }
    }while(n);
    
}

int main(){
    srand((unsigned int)time(NULL));
    menu();
    return 0;
}